<template>
  <div class="page-container">
    <div class="login-container">
      <h2>登录</h2>
      <el-form ref="loginForm" :model="loginForm" @submit.prevent="handleLogin">
        <el-form-item>
          <el-input v-model="loginForm.username" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="loginForm.password" placeholder="密码" type="password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
      <div class="extra-options">
        <el-button type="text" @click="goToRegister">没有账号？去注册</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex';

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      }
    };
  },
  methods: {
    ...mapActions(['login']),
    async handleLogin() {
      try {
        const response = await this.login(this.loginForm);
        this.$router.push("/main");
      } catch (error) {
        console.error('Login error:', error);
        // 根据实际的错误结构进行调整
        const errorMessage = error.response?.data?.message || '登录失败，请稍后再试';
        this.$message.error(errorMessage);
      }
    },
    goToRegister() {
      this.$router.push('/register');
    }
  }
};
</script>

<style>
.page-container {
  background-image: url('src/assets/【哲风壁纸】2024-06-30 12_14_11.png'); /* 替换为你的背景图路径 */
  background-size: cover; /* 背景图像填充整个容器 */
  background-position: center; /* 背景图像居中 */
  background-repeat: no-repeat; /* 背景图像不重复 */
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-container {
  width: 400px;
  padding: 40px;
  border: 1px solid #ddd;
  border-radius: 10px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  background-color: white;
  text-align: center;
}

h2 {
  color: #2c3e50;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-input {
  width: 100%;
}

.el-button {
  width: 100%;
}

.extra-options {
  margin-top: 20px;
}

.el-button--text {
  color: #3498db;
}
</style>
